package com.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import com.animation.LandingPanel;
import com.animation.PlaneRunnable;
import com.view.utils.PlaneButton;
import javax.swing.BoxLayout;

public class MainWindow extends JFrame {

	private static MainWindow instancia = null;
	private ImagePanel taxiLanePanel;
	private ControlPanel controlPanel;
	private JPanel waitPanel;
	private JPanel stackPanel;
	private JPanel runwayPanel;
	private JPanel plataformPanel;
	private JLayeredPane stackLayer;
	private JPanel landingPanel;
	private JLayeredPane skyLayer;
	private JPanel skyStruct;
	private JPanel hangarPanel;

	/**
	 * Instancia la Ventana.
	 * 
	 * @return VentanaAplicacion
	 */
	public static final MainWindow getInstance() {
		if (instancia == null) {
			instancia = new MainWindow();
		}
		return instancia;
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		super();

		initialize();
		setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		setBounds(100, 100, 1129, 750);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);

		controlPanel = new ControlPanel();

		waitPanel = new JPanel();

		stackPanel = new ImagePanel(ImagePanel.STACKLANE);
		stackPanel.setBorder(new LineBorder(new Color(0, 0, 0)));

		// frame.getContentPane().add(imagePanel, BorderLayout.CENTER);

		taxiLanePanel = new ImagePanel(ImagePanel.TAXI_LANE_BACKGROUND);
		taxiLanePanel.setLayout(new GridLayout(0, 16, 0, 0));

		runwayPanel = new ImagePanel(ImagePanel.RUNWAY);
		runwayPanel.setBorder(new LineBorder(new Color(0, 0, 0)));

		hangarPanel = new ImagePanel(ImagePanel.HANGAR);

		plataformPanel = new ImagePanel(ImagePanel.PLATAFORMA);
		plataformPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		landingPanel = new LandingPanel();
		landingPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		JPanel skyPanel = new JPanel();
		skyPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(taxiLanePanel, GroupLayout.PREFERRED_SIZE, 794, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(waitPanel, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(landingPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.UNRELATED, 60, Short.MAX_VALUE)
									.addComponent(controlPanel, GroupLayout.PREFERRED_SIZE, 290, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(hangarPanel, GroupLayout.PREFERRED_SIZE, 261, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(skyPanel, GroupLayout.PREFERRED_SIZE, 599, GroupLayout.PREFERRED_SIZE)))))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(plataformPanel, 0, 0, Short.MAX_VALUE)
						.addComponent(runwayPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(stackPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(stackPanel, GroupLayout.PREFERRED_SIZE, 714, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(plataformPanel, GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
								.addComponent(taxiLanePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addGroup(groupLayout.createSequentialGroup()
											.addGap(166)
											.addComponent(waitPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addComponent(runwayPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(hangarPanel, GroupLayout.PREFERRED_SIZE, 400, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(skyPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(6)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(landingPanel, GroupLayout.PREFERRED_SIZE, 521, GroupLayout.PREFERRED_SIZE)
										.addComponent(controlPanel, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE))))))
					.addContainerGap())
		);
		hangarPanel.setLayout(new BoxLayout(hangarPanel, BoxLayout.X_AXIS));
		
		skyPanel.setLayout(new BorderLayout(0, 0));
		
		skyLayer = new JLayeredPane();
		skyPanel.add(skyLayer);
		
		skyLayer.setPreferredSize(new Dimension(614, 126));
		stackPanel.setLayout(new BorderLayout(0, 0));

		JLabel cieloLabel = new JLabel(ImagePanel.createSky());
		cieloLabel.setSize(cieloLabel.getIcon().getIconWidth(), cieloLabel.getIcon().getIconHeight());

		skyStruct = new JPanel();
		skyStruct.setLayout(new GridLayout(0, 6, 0, 0));
		skyStruct.setSize(cieloLabel.getIcon().getIconWidth(), cieloLabel.getIcon().getIconHeight());
		skyStruct.setOpaque(false);
		
		skyLayer.add(cieloLabel, new Integer(0));
		skyLayer.add(skyStruct, new Integer(1));
		
		stackLayer = new JLayeredPane();

		Dimension dimPanel = stackPanel.getPreferredSize();

		stackLayer.setSize(dimPanel);

		stackPanel.add(stackLayer, BorderLayout.CENTER);
		plataformPanel.setLayout(new BorderLayout(0, 0));
		getContentPane().setLayout(groupLayout);

		runwayPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		

	}

	public void wipeTaxiLane() {
		taxiLanePanel.removeAll();
		taxiLanePanel.repaint();
	}

	public void wipeHoldingPattern() {
		skyStruct.removeAll();
		skyStruct.repaint();
	}
	
	public void wipePlataform() {
		plataformPanel.remove(0);
		plataformPanel.repaint();
	}
	
	

	public void drawSky(String matricula, int pasajeros, int turbocina) {
		skyStruct.add(new PlaneButton(matricula, pasajeros, turbocina, PlaneButton.PLANE_AT_SKY, false));
		validate();
	}


	public void drawTaxiLane(String matricula, int pasajeros, int turbocina) {
		taxiLanePanel.add(new PlaneButton(matricula, pasajeros, turbocina, PlaneButton.GROUND_PLANE, true));
		// SwingUtilities.updateComponentTreeUI(scrollPaneEstacionamiento);
		validate();
	}

	public void drawFirstPlaneTaxi(String matricula, int pasajeros,
			int turbocina) {
		taxiLanePanel.add(new PlaneButton(matricula, pasajeros, turbocina, PlaneButton.GROUND_PLANE, true), 0);
		// SwingUtilities.updateComponentTreeUI(scrollPaneEstacionamiento);
		validate();
	}

	public void drawPlaneToRunway(String matricula, int pasajeros, int turbocina) throws InterruptedException {
		
		plataformPanel.add(
				new PlaneButton(matricula, pasajeros, turbocina, PlaneButton.ROTATING_PLANE, false));
		plataformPanel.validate();
		
		Thread.sleep(1400);
		
		wipePlataform();
		
		plataformPanel.validate();
		
		runwayPanel.add(new PlaneButton(matricula, pasajeros, turbocina, PlaneButton.TAKE_OFF_PLANE, false));
		
		(new PlaneRunnable((ImageIcon) ((PlaneButton) runwayPanel
				.getComponent(0)).getIcon(),
				(PlaneButton) runwayPanel.getComponent(0), runwayPanel))
				.takeOff();
	}

	public void drawRetiredPlane(String matricula, int pasajeros, int turbocina){
		hangarPanel.removeAll();
		hangarPanel.add(new PlaneButton(matricula, pasajeros, turbocina, PlaneButton.GROUND_PLANE, false));
		hangarPanel.validate();
		
	}
	
	public void drawPlanetoPlataform(String matricula, int pasajeros, int turbocina){
		plataformPanel.add(
				new PlaneButton(matricula, pasajeros, turbocina, PlaneButton.GROUND_PLANE, false));
		plataformPanel.validate();
	}
	
	
	public void drawPlaneToLanding(String matricula, int pasajeros, int turbocina) {
		
		PlaneButton avion = new PlaneButton(matricula, pasajeros, turbocina, PlaneButton.LANDING_PLANE, false);
		
		GroupLayout gl_landingPanel = new GroupLayout(landingPanel);
		gl_landingPanel.setHorizontalGroup(
			gl_landingPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_landingPanel.createSequentialGroup()
					.addContainerGap(77, Short.MAX_VALUE)
					.addComponent(avion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(43))
		);
		gl_landingPanel.setVerticalGroup(
			gl_landingPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_landingPanel.createSequentialGroup()
					.addContainerGap(465, Short.MAX_VALUE)
					.addComponent(avion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		landingPanel.setLayout(gl_landingPanel);
		validate();
	}

	public void removeLastTaxiLanePlane() {
		taxiLanePanel.remove(0);
		taxiLanePanel.validate();
	}

	public JPanel getRunwayPanel() {
		return runwayPanel;
	}

	public ControlPanel getControlPanel() {
		return controlPanel;
	}

	public JLayeredPane getStackLayer() {
		return stackLayer;
	}
	public ImagePanel getTaxiLanePanel() {
		return taxiLanePanel;
	}
	public LandingPanel getLandingPanel() {
		return (LandingPanel) landingPanel;
	}
	public JPanel getSkyStruct() {
		return skyStruct;
	}
}
